/***************************************************************************************
Firm-embedded productivity and cross-country income differences
Alviarez, Cravino and Ramondo
Journal of Political Economy (2022)

Program: figure_A7_mnc_outward_manufserv.do
Date: October 2022

Description: Reproduces Figure A.7: Sectoral firm-embedded productivity and outward MNE sales.

*****************************************************************************************/

*-------------------------------------------------------------------------------
global typeden=1
include "set_directories.do"
set memory 64g
global lf "LF"

*Log
cap log close
*log using "${clogs}/figure_A7_mnc_outward_manufserv.log",replace
*-------------------------------------------------------------------------------




*---------------------------------------------		
*Output per worker 
*---------------------------------------------
use "${data}/aggregates_tfp_lp_klems_agg.dta", clear 
keep year isocode ln_ctfp ln_gdp_pw_ppp 
duplicates drop 
tempfile base0
save `base0', replace 

use "${data}/aggregates_tfp_lp_klems_agg.dta", clear 
keep year isocode type ln_LP_ppp_emp_pwt
rename ln_LP_ppp_emp_pwt ln_lp_
reshape wide ln_lp_ , i( year isocode ) j(type) string
duplicates drop 
tempfile base1
save `base1', replace 

use `base0', clear
merge 1:1 isocode year using `base1'
drop _merge 
keep if year==2016 
tempfile productivity_agg
save `productivity_agg', replace 


use "${data}/aggregates_tfp_lp_klems.dta", clear 
keep year isocode type sector1 sector ln_LP_ppp_emp_pwt
rename ln_LP_ppp_emp_pwt ln_lp_sec
duplicates drop 
merge m:1 isocode year using `productivity_agg'
keep if _merge==3
drop _merge
tempfile productivity
save `productivity', replace 

	
		
*---------------------------------------------
*Outward Multinational*
*---------------------------------------------
use "${data}/klems_oecd_unido_orbis_sales_emp_exp.dta", clear
keep year isocode  sector1 sector GO_usd_mnc_out
keep if isocode=="DK" | isocode=="JP" | isocode=="IT" | isocode=="KR" | isocode=="DE" | isocode=="FR" | isocode=="ES" |  isocode=="MX" |  isocode=="GB" |  isocode=="PL" |  isocode=="GR" |  isocode=="NL" |  isocode=="RO" |  isocode=="BE" |  isocode=="AT" |  isocode=="PT" |  isocode=="FI" |  isocode=="SE" |  isocode=="CZ" |  isocode=="SK" |  isocode=="BG" |  isocode=="HU" |  isocode=="HR" |  isocode=="LV" |  isocode=="SI" |  isocode=="LT" |  isocode=="EE" 
keep if year==2016
drop if isocode=="KR" | isocode=="MX" 

gen GO_manuf= GO_usd_mnc_out if sector1=="Manufacturing (C)"
gen GO_serv= GO_usd_mnc_out if sector1=="Market_Services (G-H-I-J-K-M-N-R-S-T)"
gen GO_tot= GO_usd_mnc_out if sector1=="Total (TOT)"
gen GO_market= GO_usd_mnc_out if sector1=="Market (MARKT)"
drop if sector1=="Non-Market Economy"
rename GO_usd_mnc_out GO_sec

gen GO_sec_fr=GO_sec if isocode=="FR"
by year sector, sort: egen xx_max=max(GO_sec_fr)
gen ln_ratio=ln(GO_sec/xx_max)
drop GO_sec_fr GO_sec xx_max
rename ln_ratio GO_sec

foreach ii in GO_manuf GO_serv GO_tot GO_market {
display "`ii'"
by year isocode, sort: egen xx=max(`ii')
drop `ii'
rename xx `ii'

gen `ii'_fr=`ii' if isocode=="FR"
by year, sort: egen xx_max=max(`ii'_fr)
gen ln_ratio=ln(`ii'/xx_max)
drop `ii'_fr `ii' xx_max
rename ln_ratio `ii'
}
*

drop if sector==sector1
merge 1:1 isocode year sector using `productivity'
drop if _merge==2
drop _merge
tempfile out_mnc
save `out_mnc', replace 


*Only aggregates* 
*------------------------------------------
use `out_mnc', clear
keep year isocode  GO_manuf GO_serv GO_tot GO_market
duplicates drop
merge 1:1 isocode year using `productivity_agg'
drop if _merge==2
drop _merge
tempfile out_mnc_sec
save `out_mnc_sec', replace



*---------------------------------------------
*PHI*
*---------------------------------------------
local nn=10 
local yy=2016
*minimum number of sectors allowed per year-isocode*/
clear all
use "${data}/estimates_sec_naics_sales_s1_base_woparent.dta", clear
keep if year==`yy'
rename DA D_A
drop if D_A==. | (D_A==0 & isocode!="FR")
tab type

keep isocode year type sector1 sector D_A
rename D_A D_A_sec
merge m:1 year isocode using "${data}/estimates_agg_naics_sales_s1_base_woparent.dta", keepusing(D_A)
rename D_A D_A_agg
drop if _merge==2
drop _merge
merge m:1 year isocode type using "${data}/estimates_aggtype_naics_sales_s1_base_woparent.dta", keepusing(D_A)
rename D_A D_A_type
drop if _merge==2
drop _merge

gen D_A_manuf=D_A_type if sector1=="Manufacturing (C)"
gen D_A_serv=D_A_type if sector1=="Market_Services (G-H-I-J-K-M-N-R-S-T)"
gen D_A_others=D_A_type if sector1=="Other_Goods (A-B-D-E-F)"
rename D_A_agg D_A_tot
drop D_A_type

foreach ii in D_A_manuf D_A_serv D_A_others  {
display "`ii'"
by year isocode, sort: egen xx=max(`ii')
drop `ii'
rename xx `ii'
}
*

*Bring MNC information
*-------------------------------- 
merge 1:1 year isocode sector using `out_mnc'
keep if _merge==3
drop _merge

gen D_A_tot_phi=(-1)*D_A_tot*(0.2)
gen b_D_A_tot_phi_`grvar0'=ln_gdp_pw_ppp - D_A_tot_phi 

gen D_A_manuf_phi=(-1)*D_A_manuf*(0.2)
gen b_D_A_manuf_phi_`grvar0'=ln_lp_manuf - D_A_manuf_phi

gen D_A_serv_phi=(-1)*D_A_serv*(0.2)
gen b_D_A_serv_phi_`grvar0'=ln_lp_serv - D_A_serv_phi

gen D_A_sec_phi=(-1)*D_A_sec*(0.2)
gen b_D_A_sec_phi_`grvar0'=ln_lp_sec - D_A_sec_phi

drop D_A_sec D_A_tot D_A_manuf D_A_serv D_A_others 
drop sector1 sector type

tempfile fig5
save `fig5', replace


*---------------------------------------------
*Sector graph: 
*---------------------------------------------
use `fig5', clear
gen rhs1=D_A_manuf_phi-D_A_serv_phi
gen rhs2=b_D_A_manuf_phi-b_D_A_serv_phi
gen lhs=GO_manuf-GO_serv
by isocode year, sort: gen a=_n==1
keep if a==1
drop a

local LHS lhs 
local RHS1 rhs1
local RHS2 rhs2
drop if `LHS'==.

sum `LHS' `RHS1' `RHS2'
local xtitle "{&Delta}`=ustrunescape("\u03D5\u0303")'{sub:manuf,n}-{&Delta}`=ustrunescape("\u03D5\u0303")'{sub:serv,n}"
local ytitle "{&Delta}rca{sub:manuf,n}-{&Delta}rca{sub:serv,n}"

*------------------------------------
local clusterlist "robust"
gen ss=1
local abslist "ss"

reghdfe `LHS' `RHS1', abs(`abslist') 
mat b = e(b)
mat V = e(V)
global c1: display %-03.2fc round(b[1,1],0.001)
global se1: display %-03.2fc round(sqrt(V[1,1]),0.001)
global rsq1: display %-03.2fc round(`e(r2)',0.001)

display $c1
display $se1
display $rsq1


global opt1 msymbol(circle_hollow) mlabel(isocode) mlabcolor(red)  msize(smaller) mlabsize(small)
global opt2 msymbol(square_hollow) mlabel(isocode) mlabcolor(blue) msize(smaller) mlabsize(small)
two (scatter `LHS' `RHS1', sort $opt1  mcolor(red)) (lfit `LHS' `RHS1', lcolor(red)) (line `RHS1' `RHS1', lcolor(none)) , ylabel(-3(1)1, format(%5.1f) labsize(medlarge)) xlabel(-0.2(0.2)0.4, format(%5.1f) labsize(medlarge)) ///
	title("", size(small)) ytitle("`ytitle'", size(medlarge)) xtitle("`xtitle'", size(medlarge)) graphregion(fcolor(white) lcolor(white)) ///
	legend(order(2 "$c1 ($se1), R2: $rsq1" ) rows(2) size(medlarge) bplace(se) ring(0) region(lwidth(none))) 

graph export "${rappendix}/fig_A7_mnc_outward_manufserv.pdf", replace


log close 
